home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / kh_graf.cpp < prev    next >
C/C++ Source or Header  |  1994-11-19  |  9KB  |  288 lines

  1. #include <stdlib.h>
  2. #include "kh_graf.h"
  3.  
  4. KH_Graf::KH_Graf(rect coordinates, char* fName, char* h, int s,
  5.     BORDERS b_type, BORDERS hdr_b_type, int pat, int hdr_pat,
  6.         int ax_col, int lab_col, int grid_style, int save_file_name)
  7.     : Window(coordinates, fName, h, s, b_type, hdr_b_type, FIXED, pat, hdr_pat),
  8.     Grafic(rect(0, 0, 10, 10), ax_col, lab_col, grid_style,
  9.            save_file_name)
  10.     {
  11.     Grafic::coord = user_screen();
  12.     Grafic::work_coord = rect(0, 0, coord.width(), coord.height());  // ???????!!!!!!!!
  13.     }
  14. /////////////////////
  15. void KH_Graf::show()
  16.     {
  17.     Window::show();
  18.     setfillstyle(SOLID_FILL, WHITE);
  19.     bar(user_screen());
  20.     }
  21. ////////////////////
  22. void KH_Graf::exe(int act)
  23.     {
  24.     e.what = act ? KEYEVENT : NOEVENT;
  25.  
  26.     switch(act)
  27.     {
  28.     case AC_CANCEL: e.key = EVENT_ESC;    break;
  29.     case AC_OK:     e.key = EVENT_F2;     break;
  30.     }
  31.     mouseHideCursor();
  32.     if(!act)
  33.     hilite();
  34.  
  35.     int on = 0;
  36.     mouseShowCursor();
  37.     while(1)
  38.     {
  39.     mouseShowCursor();
  40.     if(!act && !(e.what == MOUSEEVENT && !on))
  41.         get_event();
  42.     else
  43.         on = 1;
  44.     mouseHideCursor();
  45.  
  46.     if(e.what == KEYEVENT)
  47.         switch(e.key)
  48.         {
  49.         case EVENT_F1: global_i[0] = action_type; return;
  50.         case EVENT_ESC: global_num = 0; global_i[0] = AC_NULL; return;
  51.         case EVENT_F6:
  52.         case EVENT_F10:
  53.         case EVENT_TAB:
  54.         case EVENT_ALT_F3:
  55.         case EVENT_ALT_F4:
  56.         case EVENT_ALT_TAB:
  57.             unhilite(); global_num = 0;
  58.             global_i[0] = 0; return;
  59.         case EVENT_F2:
  60.         case EVENT_RETURN :  unhilite(); global_num = 0;
  61.             global_i[0] = action_type; return;
  62.         }
  63.     else
  64.         {
  65.         if(!mouse_in(e.where()))
  66.         {
  67.         unhilite();
  68.         global_num = 0; global_i[0] = AC_NULL;
  69.         return;
  70.         }   // outside of menu box
  71.         }
  72.     if(act)    // leave menu and return to the object which calls it
  73.         {      // after single processing of "act" command
  74.         global_num = 0;
  75.         return;
  76.         }
  77.     }
  78.     }
  79. ///////////////////
  80. void KH_Graf::touch(int i)
  81.     {
  82.     char** lab_x1 = global_i[2] == -1 ? NULL : global + 10;
  83.     int num_lab_x1 = global_i[2] == -1 ? 0 : global_i[2];
  84.     int sub_num_lab_x1 = global_i[3] == -1 ? 0 : global_i[3];
  85.     int* tks_x1 = global_i[2] == -1 ? NULL : global_i + 20;
  86.     int* s_tks_x1 = global_i[3] == -1 ? NULL : global_i + 20 + num_lab_x1;
  87.  
  88.     char** lab_y1 = global_i[5] == -1 ? NULL : global + 30;
  89.     int num_lab_y1 = global_i[5] == -1 ? 0 : global_i[5];
  90.     int sub_num_lab_y1 = global_i[6] == -1 ? 0 : global_i[6];
  91.     int* tks_y1 = global_i[5] == -1 ? NULL : global_i + 40;
  92.     int* s_tks_y1 = global_i[6] == -1 ? NULL : global_i + 40 + num_lab_y1;
  93.  
  94. /*
  95.     char** lab_x2 = global_i[8] == -1 ? NULL : global + 50;
  96.     int num_lab_x2 = global_i[8] == -1 ? 0 : global_i[8];
  97.     int sub_num_lab_x2 = global_i[9] == -1 ? 0 : global_i[9];
  98.     int* tks_x2 = global_i[8] == -1 ? NULL : global_i + 60;
  99.     int* s_tks_x2 = global_i[9] == -1 ? NULL : global_i + 60 + num_lab_x2;
  100.  
  101.     char** lab_y2 = global_i[11] == -1 ? NULL : global + 70;
  102.     int num_lab_y2 = global_i[11] == -1 ? 0 : global_i[11];
  103.     int sub_num_lab_y2 = global_i[12] == -1 ? 0 : global_i[12];
  104.     int* tks_y2 = global_i[11] == -1 ? NULL : global_i + 80;
  105.     int* s_tks_y2 = global_i[12] == -1 ? NULL : global_i + 80 + num_lab_y2;
  106. */
  107.     char *endptr;
  108.     double start, end, startx, endx, starty, endy;
  109.     switch(i)
  110.         {
  111.         case 0:
  112.             delete global[0];
  113.             global[0] = strdup("KH_GRAF");
  114.             break;
  115.         case -1:                   // Options set
  116.         case -2:
  117.             if(global_i[1] & 1)   // 1-st x axe present
  118.                 {
  119.                 start = startx = strtod(global[1], &endptr);
  120.                 end = endx = strtod(global[2], &endptr);
  121.  
  122.                 set_axe(HORIZ1, work_coord.width() - 1, start, end,
  123.             num_lab_x1, tks_x1, sub_num_lab_x1, s_tks_x1, lab_x1,
  124.             global_i[4]);
  125.         }
  126.             if(global_i[1] & 2)   // 1-st y axe present
  127.                 {
  128.                 start = starty = strtod(global[3], &endptr);
  129.                 end = endy = strtod(global[4], &endptr);
  130.  
  131.                 set_axe(VERT1, work_coord.height() - 1, start, end,
  132.             num_lab_y1, tks_y1, sub_num_lab_y1, s_tks_y1, lab_y1,
  133.             global_i[7]);
  134.         }
  135.         if(i == -2)
  136.             {
  137.             setfillstyle(SOLID_FILL, WHITE);
  138.             bar(user_screen());
  139.  
  140.             loc z = get_zero(startx, starty, endx, endy);
  141.             setviewport(coord.origin.X, coord.origin.Y, coord.corner.X,
  142.             coord.corner.Y, 1);
  143.             grid();
  144.             setcolor(BLACK);     // FOR CROSS
  145.             show_axes();
  146.             setviewport(0, 0, getmaxx(), getmaxy(), 1);
  147.             }
  148.             break;
  149.     }
  150.     }
  151. ////////////////////////////////////////////
  152. int KH_Graf::calc_work_rect()
  153.     {
  154.     if(horiz_axe->text_dir == VERT_DIR)
  155.         work_coord.origin.X += 5 * pScreenSet->cell_width;
  156.     else
  157.         {
  158.         int l = 0;
  159.         for(int i = 0; i < horiz_axe->ticks_no; i++)
  160.             l = max(strlen(horiz_axe->labels[i]), l);
  161.         l += 2;
  162.         work_coord.origin.X += l * pScreenSet->cell_width;
  163.     }
  164.  
  165.     if(vert_axe->text_dir == HORIZ_DIR)
  166.         work_coord.corner.Y -= 2 * pScreenSet->cell_height;
  167.     else
  168.         {
  169.         int l = 0;
  170.         for(int i = 0; i < vert_axe->ticks_no; i++)
  171.             l = max(strlen(vert_axe->labels[i]), l);
  172.         work_coord.corner.Y -= l * pScreenSet->cell_height;
  173.     }
  174.     if(horiz_axe_2 != NULL && horiz_axe_2->text_dir == VERT_DIR)
  175.         work_coord.corner.X -= 5 * pScreenSet->cell_width;
  176.     else if(horiz_axe_2 != NULL)
  177.         {
  178.         int l = 0;
  179.         for(int i = 0; i < horiz_axe_2->ticks_no; i++)
  180.             l = max(strlen(horiz_axe_2->labels[i]), l);
  181.         l += 2;
  182.         work_coord.corner.X -= l * pScreenSet->cell_width;
  183.     }
  184.  
  185.     if(vert_axe_2 != NULL && vert_axe_2->text_dir == HORIZ_DIR)
  186.         work_coord.origin.Y += 2 * pScreenSet->cell_height;
  187.     else if(vert_axe_2 != NULL)
  188.         {
  189.         int l = 0;
  190.         for(int i = 0; i < vert_axe_2->ticks_no; i++)
  191.             l = max(strlen(vert_axe_2->labels[i]), l);
  192.         work_coord.origin.Y += l * pScreenSet->cell_height;
  193.     }
  194.  
  195.     if(work_coord.width() < 5 || work_coord.height() < 3)
  196.         return 0;
  197.  
  198.     return 1;
  199.     }
  200. ////////////////////////////
  201. /*
  202. void main()
  203.     {
  204.     if(!init_KNOW_HOW())
  205.     return;
  206.     drawTool = new KH_Paint();   // See KHPAINT.H
  207.  
  208.     setfillstyle(SOLID_FILL, pColorSet->colors.BAK_COLOR);
  209.     bar(0, 0, getmaxx(), getmaxy());
  210.  
  211.     static double x_ar[] = { -.80, -.60, -.40, -.20, 0, .20, .40, .60, .80 };
  212.     static double y1_ar[] = { -200, -150, -100, -50, 0, 50, 100, 150, 200 };
  213.     static double y2_ar[] = { -100, 100, -20, -5, 19, 100, 200, 100, 100 };
  214.  
  215.  
  216.     KH_Graf* tg = new KH_Graf(rect(5, 5, 60, 20), "graf.pcy", "Grafic view",
  217.            6, SHOW_BORDER, SHOW_BORDER, 17, 20,
  218.            LIGHTGREEN, LIGHTRED, 3, 0);
  219.  
  220.     tg->show_window();
  221.  
  222.     global_i[1] = 3;      //            axes used : x1-2 and y1-2.
  223.     global_i[2] = -1;     //            x1, ticks, autocalculation.
  224.     global_i[3] = -1;     //            x1, number of sub-ticks = 0.
  225.     global_i[4] = 1;      //            x1, horiz. labels.
  226.  
  227.     global_i[5] = -1;     //            y1, autocalculation.
  228.     global_i[6] = -1;     //            y1, number of sub-ticks = 0.
  229.     global_i[7] = 0;      //            y1, vert. orientation of labels.
  230.  
  231.  
  232.     delete global[1];
  233.     delete global[2];
  234.     delete global[3];
  235.     delete global[4];
  236.  
  237.     global[1] = strdup("-1");   //   min and max double values for all axes.
  238.     global[2] = strdup("1");
  239.     global[3] = strdup("-300");
  240.     global[4] = strdup("600");
  241.  
  242.     tg->touch(-1);              // 1-st pass.
  243.  
  244.     tg->calc_work_rect();
  245.     tg->touch(-2);              // Verification after change of work_rect.
  246.  
  247.     tg->get_x_array(0, 9, x_ar);
  248.     tg->get_y_array(0, 9, y1_ar);
  249.     tg->get_x_array(1, 9, x_ar);
  250.     tg->get_y_array(1, 9, y2_ar);
  251.     tg->get_x_array(2, 9, x_ar);
  252.     tg->get_y_array(2, 9, y2_ar);
  253.  
  254.     tg->arrays[0]->graf->set_type(BAR, STACKED_BAR_GRAF);
  255.     tg->arrays[1]->graf->set_type(BAR, STACKED_BAR_GRAF);
  256.     tg->arrays[2]->graf->set_type(BAR, STACKED_BAR_GRAF);
  257.  
  258.     int w = tg->bar_width();
  259.     tg->arrays[0]->graf->set_param(RED, GREEN, w, SLASH_FILL);
  260.     tg->arrays[1]->graf->set_param(RED, CYAN, w, LINE_FILL);
  261.     tg->arrays[2]->graf->set_param(RED, RED, w, XHATCH_FILL);
  262.  
  263.     setviewport(rect(tg->user_screen().origin + tg->get_work_coord().origin,
  264.         tg->user_screen().origin + tg->get_work_coord().corner), 1);
  265.  
  266.     loc z = tg->get_zero(-1.00, -300, 1.00, 600) - tg->get_work_coord().origin;
  267.     int* array = tg->get_stacked(0);
  268.     tg->arrays[0]->graf->show(tg->arrays[0]->data_x, array, 9, z.X, z.Y);
  269.     delete array;
  270.     array = tg->get_stacked(1);
  271.     tg->arrays[1]->graf->show(tg->arrays[1]->data_x, array, 9, z.X, z.Y);
  272.     delete array;
  273.     array = tg->get_stacked(2);
  274.     tg->arrays[2]->graf->show(tg->arrays[2]->data_x, array, 9, z.X, z.Y);
  275.     delete array;
  276.  
  277.     setviewport(0, 0, getmaxx(), getmaxy(), 1);
  278.  
  279.  
  280.     tg->exe();
  281.     tg->hide();
  282.     delete tg;
  283.     close_KNOW_HOW();
  284.     delete drawTool;   // See KHPAINT.H
  285.     closegraph();
  286.  
  287.     }
  288. */